<?php
/**
 *	Actions for Bugs.
 *	@package		tools.BugTracker
 *	@author			Christian W�rker <Christian.Wuerker@CeuS-Media.de>
 *	@since			01.11.2005
 *	@version		1.0
 */
/**
 *	Actions for Bugs.
 *	@package		tools.BugTracker
 *	@extends		CMF_Krypton_Core_DefinitionAction
 *	@uses			User
 *	@uses			Bug
 *	@uses			Comment (not yet)
 *	@author			Christian W�rker <Christian.Wuerker@CeuS-Media.de>
 *	@since			01.11.2005
 *	@version		1.0
 */
class Action_Bug_Report extends CMF_Krypton_Core_DefinitionAction
{
	/**
	 *	Constructor.
	 *	@access		public
	 *	@return		void
	 */
	public function __construct()
	{
		parent::__construct();
		$this->loadLanguage( 'bug', 'bug' );
		$this->addAction( 'addBug' );
	}

	/**
	 *	Adds a Bug Report and sends eMails to all Managers.
	 *	@access		protected
	 *	@return		void
	 */
	protected function addBug()
	{
		$request	= $this->registry->get( 'request' );
		$mail		= $this->registry->get( 'mail' );
		$words		= $this->words['bug']['msg'];
		try
		{
			Logic_Bug::addBug( $request->getAll(), 'add_' );
			$request->remove( 'add_title' );
			$request->remove( 'add_description' );
			$request->remove( 'add_type' );
			$request->remove( 'add_priority' );
			$this->messenger->noteSuccess( $words['success1'] );
		#	$this->sendMail();
		}
		catch( Exception $e )
		{
			$this->handleException( $e, 'bug', 'addBug' );
		}
	}

	private function sendMail( $bugId )
	{
		$dataBug	= Logic_Bug::getBugData( $bugId );
		$auth		= $this->registry->get( 'auth' );
		$from		= $auth->getCurrentUser();
		$user		= new User();
		$users		= $user->getAllData();
		$users		= $users[$user->getTableName()];
		$view		= new BugViews();
		$info		= $view->buildInformationBlock( $bugdata );
		$subject	= "Bug #".$bugdata['bugId']." added.";
		$body		= "Bug #".$bugdata['bugd']." has been added.\n\n".$info;
		foreach( $users as $entry )
		{
			if( $user->hasRole( $entry['userId'], 2 ) )		//  is a Manager
			{
				if( $entry['userId'] != $from )
				{
					if( $mail->sendMail( $from, $entry['userId'], $subject, $body ) )
						$this->messenger->noteSuccess( $words['success9'], $entry['username'] );
					else
						$this->messenger->noteError( $words['error9'], $entry['username'] );
				}
			}
		}
		if( $auth->hasRight( "bug", "navigate" ) )
			$this->restart( "?link=bug&bugId=".$bugId );
	}
}
?>